#include <X11/Xlib.h>
#include <stdlib.h>
#include <stdio.h>




int client_msg(Display *disp, Window win, char *msg, /* {{{ */       
        unsigned long data0, unsigned long data1,                           
        unsigned long data2, unsigned long data3,                           
        unsigned long data4) {                                              
    XEvent event;                                                           
    long mask = SubstructureRedirectMask | SubstructureNotifyMask;          
                                                                            
    event.xclient.type = ClientMessage;                                     
    event.xclient.serial = 0;                                               
    event.xclient.send_event = True;                                        
    event.xclient.message_type = XInternAtom(disp, msg, False);             
    event.xclient.window = win;                                             
    event.xclient.format = 32;                                              
    event.xclient.data.l[0] = data0;                                        
    event.xclient.data.l[1] = data1;                                        
    event.xclient.data.l[2] = data2;                                        
    event.xclient.data.l[3] = data3;                                        
    event.xclient.data.l[4] = data4;                                        
                                                                            
    if (XSendEvent(disp, DefaultRootWindow(disp), False, mask, &event)) {   
        return EXIT_SUCCESS;                                                
    }                                                                       
    else {                                                                  
        fprintf(stderr, "Cannot send %s event.\n", msg);                    
        return EXIT_FAILURE;                                                
    }                                                                       
}/*}}}*/                                                                    

int activate_window (Display *disp, Window win) {
    unsigned long *desktop;

#if 0
    /* desktop ID */
    if ((desktop = (unsigned long *)get_property(disp, win,
            XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) {
        if ((desktop = (unsigned long *)get_property(disp, win,
                XA_CARDINAL, "_WIN_WORKSPACE", NULL)) == NULL) {
            p_verbose("Cannot find desktop ID of the window.\n");
        }
    }

    if (switch_desktop && desktop) {
        if (client_msg(disp, DefaultRootWindow(disp), 
                    "_NET_CURRENT_DESKTOP", 
                    *desktop, 0, 0, 0, 0) != EXIT_SUCCESS) {
            p_verbose("Cannot switch desktop.\n");
        }
        g_free(desktop);
    }
#endif

    client_msg(disp, win, "_NET_ACTIVE_WINDOW", 
            0, 0, 0, 0, 0);
    XMapRaised(disp, win);

    return EXIT_SUCCESS;
}/*}}}*/

int main(int argc, char *argv[]) {
    unsigned long wid = 0;
    printf("argc: %d\n", argc);
    if(argc==2) {
        wid = strtoul(argv[1], NULL, 0);
    }
    Display *dpy = XOpenDisplay(":0");
    printf("activate window: %#lx, res:%d\n", wid, activate_window(dpy, (Window)wid));   
    XCloseDisplay(dpy);
    return 0;
}   
